package com.neusoft.movie.v_movies.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;

import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.activity.SearchMovieActivity;
import com.neusoft.movie.v_movies.adpater.MoviesAdapter;
import com.neusoft.movie.v_movies.javabean.MoviesBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.MoviesModel;

import java.util.List;

/**
 * Created by Administrator on 2017/12/25.
 */

public class MoviesFragment extends BaseFragment implements RetrofitListener<List<MoviesBean>> {
    RecyclerView recyclerView;
    private ImageView button_search;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        setLayout_file(R.layout.mtab01);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    void initEvents() {
        button_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MoviesFragment.this.getActivity(), SearchMovieActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    void initViews() {
        recyclerView = (RecyclerView)view.findViewById(R.id.movies_list);
        //recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(1);
        recyclerView.addItemDecoration(decoration);

        button_search=(ImageView) view.findViewById(R.id.button_search);

    }
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public SpacesItemDecoration(int space) {
            this.space=space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }
    @Override
    void initData() {
        MoviesModel moviesModel = new MoviesModel();
        moviesModel.getMovieslist(this);

    }


    @Override
    public void onSuccess(List<MoviesBean> movies, int flag) {
        MoviesAdapter adapter = new MoviesAdapter(getActivity(),movies);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFailed() {

    }

}
